package com.ebelter.temperaturegun.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.LoadingDialog;
import com.ebelter.temperaturegun.utils.StatusBarUtil;


public abstract class TBaseActivity extends Activity {


    public static final String TAG = "TBaseActivity";
    protected BaseHandle mMainBaseHandle;
    private LoadingDialog loadingDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setContentViewId());

        StatusBarUtil.setStatusBarDarkTheme(this, false);//设置状态栏的字体为白色
        StatusBarUtil.setColor(this, Color.parseColor("#ff000000"),0);//设置状态栏颜色
        changeStatusBar();

        if (isInitMainBaseHandle()){
            mMainBaseHandle=new BaseHandle(this);
            mMainBaseHandle.setMsgCallBack(new BaseHandle.IHandleMsgCallBack() {
                @Override
                public void handleMsg(Message msg) {
                    activityHandleMsg(msg);
                }
            });
        }

        checkPermissions();




        initView();
        initData();
    }


    private static final String[] permissions=new String[]{
            Manifest.permission.BLUETOOTH,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.BLUETOOTH_ADMIN};

    private void checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (String p:permissions){
                // 首先是判断
                if (ContextCompat.checkSelfPermission(this, p) != PackageManager.PERMISSION_GRANTED) {
                    LogUtils.i(TAG,"权限"+p+"没有授权成功1");
                    // Should we show an explanation?
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, p)) {
                        // 显示给用户的解释
                        LogUtils.i(TAG,"权限"+p+"没有授权成功2--shouldShowRequestPermissionRationale");
                        ActivityCompat.requestPermissions(this, new String[]{p}, 1);
                    } else {
                        // No explanation needed, we can request the permission.
                        ActivityCompat.requestPermissions(this, new String[]{p}, 1);
                        LogUtils.i(TAG,"权限"+p+"没有授权成功3--requestPermissions--"+p);
                    }
                }else {
                    LogUtils.i(TAG,"权限"+p+"-----授权成功");

                }
            }
        }


    }


    protected void changeStatusBar() {
    }

    protected void initData() {
    }

    protected Bundle getBundle() {
        return getIntent().getBundleExtra("bundle");
    }

    protected void initView() {
    }

    protected abstract int setContentViewId();
    //是否要初始化主线程Handle
    protected  boolean isInitMainBaseHandle(){return false;}
    //handle处理
    protected void activityHandleMsg(Message msg) {}

    protected void startActivity(Class<?> cls,Bundle bundle){
        Intent intent=new Intent(this,cls);
        if (bundle!=null){
            intent.putExtra("bundle",bundle);
        }
        startActivity(intent);
    }


    public void showLoadingDialog(String tip){
        if (loadingDialog==null){
            loadingDialog=new LoadingDialog(this);
        }

        loadingDialog.show();
        loadingDialog.setTip(tip);

    }

    public void dissLoadingDialog(){
        if (loadingDialog!=null){
            loadingDialog.dismiss();
        }
    }
}
